﻿using System;
using System.Collections.Generic;
using System.Text;
using ITAP.Core.Modelling.Primitives;

namespace ITAP.ShprossesBuilder.Modelling.Components.Actions
{
    public class ActionBreakToPieces : IComponentAction, IModelVisitor
    {
        private int numberOfPieces;
        public ActionBreakToPieces(int numberOfPieces)
        {
            this.numberOfPieces = numberOfPieces;
        }
        #region IEndedComponentAction Members

        public bool Action(IComponent component)
        {
            try
            {
                component.AcceptVisitor(this);
            }
            catch
            {
                return false;
            }
            return true;
        }

        #endregion

        #region IModelVisitor Members

        // Визитим компоненты для разбиения. Имплементим только необходимые на данный момент, остальные оставляем.
        public void Visit(Model model)
        {
            throw new NotImplementedException();
        }

        public void Visit(Connector connector)
        {
            throw new NotImplementedException();
        }

        public void Visit(Shpross shpross)
        {
            List<IComponent> newShprosses = new List<IComponent>();
            IEndedPrimitive[] newPrimitives = ((IEndedPrimitive)shpross.Primitives[0]).BreakToPieces(numberOfPieces, 0);
            for (int i = 0; i < newPrimitives.Length; i++)
            {
                IWidePrimitive primitive = (IWidePrimitive)newPrimitives[i];
                Shpross newShpross = ComponentFactory.CreateShprossWithSpacerConnectors(primitive.Width, 
                                            primitive.Height, 
                                            primitive.StartPoint, 
                                            primitive.EndPoint, 
                                            shpross.StartSpacerConnector, 
                                            shpross.EndSpacerConnector, 
                                            shpross.Article + " Clone #" + i.ToString(),
                                            shpross.Color, shpross.BorderColor, shpross.UniqueId, shpross.AcceptedUniqueIdsToConnect.ToArray());

                //shpross.ValidateSpacerConnectorsPositions();
               // newShpross.FitStartSpacerConnector();
               // newShpross.FitEndSpacerConnector();

                newShprosses.Add(newShpross);
            }
            IComponent shprossParent = shpross.Parent;
            if (shprossParent != null)
            {
                // При удалении внутреннего элемента удаляются и все коннекты элемента с другими элементами.
                shprossParent.RemoveInnerComponent(shpross);
                // Если есть предок, то надо в него добавить вновь созданные элементы.
                foreach (IComponent newShpross in newShprosses)
                {
                  //  (newShpross as Shpross).ValidateSpacerConnectorsPositions();
                    shprossParent.AddInnerComponent(newShpross);
                }
            }
            else
                throw new Exception("У профиля, который разбивается, должен существовать предок!");            
        }

        public void Visit(Spacer spacer)
        {
            throw new NotImplementedException();
        }

        public void Visit(ShprossConnectorStub stub)
        {
            throw new NotImplementedException();
        }

        public void Visit(ShprossConnector shprossConnector)
        {
            throw new NotImplementedException();
        }

        public void Visit(SpacerElement spacerElement)
        {
            throw new NotImplementedException();
        }

        public void Visit(SpacerConnector spacerConnector)
        {
            throw new NotImplementedException();
        }

        public void Visit(ModelCell modelCell)
        {
            throw new NotImplementedException();
        }

        public void Visit(ModelContainer modelContainer)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(StickConnector stickConnector)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(SnapOnCap snapOnCap)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossEndToEndConnector endToEndConnector)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(ShprossChain shprossChain)
        {
         //   throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(SpacerContainer spacerContainer)
        {
          //  throw new NotImplementedException();
        }

        #endregion

        #region IModelVisitor Members


        public void Visit(CrossConnector crossConnector)
        {
      //      throw new NotImplementedException();
        }

        #endregion
    }
}
